import { DatabaseHelper } from './DatabaseHelper.js';
import { UserDataResp } from './model/UserDataResp';
import { user } from './model/user';

let myHandler = async function (event, context, callback, logger) {
  logger.info(JSON.stringify(event));
  let userId: string = event.body ? JSON.parse(event.body).userId : event.userId;
  let msg;
  try {
    const databaseHelper = new DatabaseHelper(context, logger);
    const user:user = await databaseHelper.queryUser(userId);
    logger.info('user'+JSON.stringify(user));
    if (user) {
        const likedIds = await databaseHelper.queryLikedIds(userId);
        const collectedIds = await databaseHelper.queryCollectedIds(userId);
        let userData = new UserDataResp(
          user.getId(),
          user.getUsername(),
          user.getPortrait(),
          user.getDescription(),
          user.getPhone(),
          likedIds,
          collectedIds)
      callback({
        code: 0,
        message: 'ok',
        data: userData,
      });
    }
  } catch (err) {
    logger.error("func error:" + err.message + " stack:" + err.stack);
    callback({
      code: 1,
      message: '查询失败',
      data: err,
    });
  }
};

export { myHandler };